home *** CD-ROM | disk | FTP | other *** search
/ Cream of the Crop 1 / Cream of the Crop 1.iso / PROGRAM / DKBSRC.ARJ / PRIOQ.C < prev    next >
C/C++ Source or Header  |  1991-05-04  |  5KB  |  199 lines

  1. /*****************************************************************************
  2. *
  3. *                                    prioq.c
  4. *
  5. *   from DKBTrace (c) 1990  David Buck
  6. *
  7. *  This module implements a priority queue using a heap.
  8. *
  9. * This software is freely distributable. The source and/or object code may be
  10. * copied or uploaded to communications services so long as this notice remains
  11. * at the top of each file.  If any changes are made to the program, you must
  12. * clearly indicate in the documentation and in the programs startup message
  13. * who it was who made the changes. The documentation should also describe what
  14. * those changes were. This software may not be included in whole or in
  15. * part into any commercial package without the express written consent of the
  16. * author.  It may, however, be included in other public domain or freely
  17. * distributed software so long as the proper credit for the software is given.
  18. *
  19. * This software is provided as is without any guarantees or warranty. Although
  20. * the author has attempted to find and correct any bugs in the software, he
  21. * is not responsible for any damage caused by the use of the software.  The
  22. * author is under no obligation to provide service, corrections, or upgrades
  23. * to this package.
  24. *
  25. * Despite all the legal stuff above, if you do find bugs, I would like to hear
  26. * about them.  Also, if you have any comments or questions, you may contact me
  27. * at the following address:
  28. *
  29. *     David Buck
  30. *     22C Sonnet Cres.
  31. *     Nepean Ontario
  32. *     Canada, K2H 8W7
  33. *
  34. *  I can also be reached on the following bulleton boards:
  35. *
  36. *     OMX              (613) 731-3419
  37. *     Mystic           (613) 596-4249  or  (613) 596-4772
  38. *
  39. *  Fidonet:   1:163/109.9
  40. *  Internet:  dbuck@ccs.carleton.ca
  41. *  The "You Can Call Me RAY" BBS    (708) 358-5611
  42. *
  43. *  IBM Port by Aaron A. Collins. Aaron may be reached on the following BBS'es:
  44. *
  45. *     The "You Can Call Me RAY" BBS (708) 358-5611
  46. *     The Information Exchange BBS  (708) 945-5575
  47. *
  48. *****************************************************************************/
  49.  
  50. #include "frame.h"
  51. #include "dkbproto.h"
  52.  
  53. #define NUMBER_OF_PRIOQS 20
  54. #define NUMBER_OF_INTERSECTIONS 20
  55.  
  56. PRIOQ *prioqs;
  57.  
  58. void pq_init ()
  59.    {
  60.    register int i;
  61.    PRIOQ *new_pq;
  62.  
  63.    prioqs = NULL;
  64.  
  65.    for (i = 0 ; i < NUMBER_OF_PRIOQS; i++)
  66.       {
  67.       if ((new_pq = (PRIOQ *) malloc (sizeof (PRIOQ))) == NULL) {
  68.          fprintf (stderr, "\nCannot allocate queues");
  69.          exit(1);
  70.          }
  71.       
  72.       new_pq -> next_pq = prioqs;
  73.       prioqs = new_pq;
  74.  
  75.       if ((new_pq -> queue = (INTERSECTION *)
  76.             malloc (128 * sizeof (INTERSECTION))) == NULL) {
  77.          fprintf (stderr, "\nCannot allocate queue entries");
  78.          exit(1);
  79.          }
  80.       }
  81.    }
  82.  
  83. PRIOQ *pq_alloc()
  84.    {
  85.    PRIOQ *allocated_queue;
  86.  
  87.    if (prioqs == NULL) {
  88.       fprintf (stderr, "\nOut of prioqs");
  89.       exit(1);
  90.       }
  91.  
  92.    allocated_queue = prioqs;
  93.    prioqs = allocated_queue -> next_pq;
  94.    return (allocated_queue);
  95.    }
  96.  
  97. void pq_free (pq)
  98.    PRIOQ *pq;
  99.    {
  100.    pq -> next_pq = prioqs;
  101.    prioqs = pq;
  102.    }
  103.  
  104. PRIOQ *pq_new (index_size)
  105.    int index_size;
  106.    {
  107.    PRIOQ *pq;
  108.  
  109.    if (index_size > 256)
  110.       return (NULL);
  111.  
  112.    if ((pq = pq_alloc()) == NULL)
  113.       return (NULL);
  114.  
  115.    pq -> queue_size = index_size;
  116.    pq -> current_entry = 0;
  117.    return (pq);
  118.    }
  119.  
  120. void pq_balance(q, entry_pos1)
  121.    PRIOQ *q;
  122.    unsigned int entry_pos1;
  123.    {
  124.    register INTERSECTION *entry1, *entry2;
  125.    INTERSECTION temp_entry;
  126.    register unsigned int entry_pos2;
  127.  
  128.    entry1 = &q->queue[entry_pos1];
  129.  
  130.    if ((entry_pos1 * 2 < q->queue_size)
  131.        && (entry_pos1 * 2 <= q -> current_entry))
  132.       {
  133.       if ((entry_pos1*2+1 > q->current_entry) ||
  134.           (q->queue[entry_pos1*2].Depth < q->queue[entry_pos1*2+1].Depth))
  135.          entry_pos2 = entry_pos1*2;
  136.       else
  137.          entry_pos2 = entry_pos1*2+1;
  138.  
  139.       entry2 = &q->queue[entry_pos2];
  140.  
  141.       if (entry1->Depth > entry2->Depth) {
  142.          temp_entry = *entry1;
  143.          *entry1 = *entry2;
  144.          *entry2 = temp_entry;
  145.          pq_balance (q, entry_pos2);
  146.          }
  147.       }
  148.  
  149.    if (entry_pos1 / 2 >= 1 )
  150.       {
  151.       entry_pos2 = entry_pos1 / 2;
  152.       entry2 = &q->queue[entry_pos2];
  153.       if (entry1->Depth < entry2->Depth) {
  154.          temp_entry = *entry1;
  155.          *entry1 = *entry2;
  156.          *entry2 = temp_entry;
  157.          pq_balance (q, entry_pos2);
  158.          }
  159.       }
  160.    }
  161.  
  162. void pq_add (q, entry)
  163.    PRIOQ *q;
  164.    INTERSECTION *entry;
  165.    {
  166.    q -> current_entry++;
  167.    if (q -> current_entry >= q -> queue_size)
  168.       q -> current_entry--;
  169.  
  170.    q -> queue [q -> current_entry] = *entry;
  171.    pq_balance (q, q -> current_entry);
  172.    }
  173.  
  174. INTERSECTION *pq_get_highest(q)
  175.    PRIOQ *q;
  176.    {
  177.    if (q -> current_entry >= 1)
  178.       return (&(q -> queue[1]));
  179.    else
  180.       return (NULL);
  181.    }
  182.  
  183. int pq_is_empty(q)
  184.    PRIOQ *q;
  185.    {
  186.    if (q -> current_entry < 1)
  187.       return (TRUE);
  188.    else
  189.       return (FALSE);
  190.    }
  191.  
  192. void pq_delete_highest (q)
  193.    PRIOQ *q;
  194.    {
  195.    q -> queue[1] = q -> queue[q -> current_entry--];
  196.    pq_balance (q, 1);
  197.    }
  198.  
  199.